package model

import (
	"blogdemo/gin/errmsg"
	"github.com/jinzhu/gorm"
)

type Comment struct {
	gorm.Model
	ArticleId    int    `gorm:"type:bigint(20) unsigned; not null" json:"articleId"`
	ArticleTitle string `gorm:"type:varchar(20); not null" json:"articleTitle"`
	UserId       int    `gorm:"type:bigint(20) unsigned; not null" json:"userId"`
	Username     string `gorm:"type:varchar(20); not null" json:"username"`
	Title        string `gorm:"type:varchar(20); not null" json:"title"`
	Content      string `gorm:"type:longtext; not null" json:"content"`
}

// CreateComment 添加新的评论到comment表中
func CreateComment(theComment *Comment) int {
	err := db.Create(&theComment).Error
	if err != nil {
		return errmsg.FAIL
	}
	return errmsg.SUCCESS
}

// GetCommentArticleId 以文章ID搜素文章评论
func GetCommentArticleId(id string) ([]Comment, bool) {
	var comment []Comment
	db.Where("article_id = ?", id).Find(&comment)
	if len(comment) != 0 {
		return comment, true
	} else {
		return comment, false
	}
}

// GetCommentTitle 以文章标题搜素文章评论
func GetCommentTitle(title string) ([]Comment, bool) {
	var comment []Comment
	db.Where("article_title = ?", title).Find(&comment)
	if len(comment) != 0 {
		return comment, true
	} else {
		return comment, false
	}
}

// GetCommentUser 以用户名搜素文章评论
func GetCommentUser(name string) ([]Comment, bool) {
	var comment []Comment
	db.Where("username = ?", name).Find(&comment)
	if len(comment) != 0 {
		return comment, true
	} else {
		return comment, false
	}
}

// GetCommentUserId 以用户ID搜素文章评论
func GetCommentUserId(id string) ([]Comment, bool) {
	var comment []Comment
	db.Where("user_id = ?", id).Find(&comment)
	if len(comment) != 0 {
		return comment, true
	} else {
		return comment, false
	}
}
